import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie';  


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/dashboard'
    },
    {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import( '@/views/dashboard.vue')
    },
    {
        path: '/home',
        redirect: '/index',
    },
    {
        path: '/audit',
        name: 'Audit',
        component: () => import('@/views/auditServices/Index'),
        meta: { title: '审计业务全流程', icon: 'file-alt' },
        children: [
          {
            path: 'index',
            name: 'Index',
            component: () => import('@/views/auditServices/Index'),
            meta: {title: '审计业务全流程',},
          },
        ],
    },
    {
        path: '/government',
        name: 'Government',
        component: () => import('@/views/governmentWork/Index'),
        meta: { title: '机关办公全场景', icon: 'file-alt' },
        children: [
          {
            path: '/',
            name: 'Index',
            component: () => import('@/views/governmentWork/component/Document'),
            meta: {title: '错别字检索',},
          },
          {
            path: 'document',
            name: 'Document',
            component: () => import('@/views/governmentWork/component/Document'),
            meta: {title: '错别字检索',},
          },
          {
            path: 'evidence',
            name: 'Evidence',
            component: () => import('@/views/governmentWork/component/Evidence'),
            meta: {title: '取证单格式化',},
          },
          {
            path: 'workpaper',
            name: 'Workpaper',
            component: () => import('@/views/governmentWork/component/Workpaper'),
            meta: {title: '',},
          },
          {
            path: 'auditReport',
            name: 'AuditReport',
            component: () => import('@/views/governmentWork/component/AuditReport'),
            meta: {title: '',},
          },
          {
            path: 'solicitingOpinions',
            name: 'SolicitingOpinions',
            component: () => import('@/views/governmentWork/component/SolicitingOpinions'),
            meta: {title: '',},
          },
          {
            path: 'documentDesensitization',
            name: 'DocumentDesensitization',
            component: () => import('@/views/governmentWork/component/DocumentDesensitization'),
            meta: {title: '',},
          },
          {
            path: 'fileLocalFormat',
            name: 'FileLocalFormat',
            component: () => import('@/views/governmentWork/component/FileLocalFormat'),
            meta: {title: '',},
          },
          
        ],
    },
    {
        path: '/setting',
        name: 'Setting',
        component: () => import('@/views/setting/Index'),
        meta: { title: '系统设置', icon: '' },
        children: [
          {
            path: 'index',
            name: 'Index',
            component: () => import('@/views/setting/Index'),
            meta: {title: '系统设置',},
          },
        ],
    },
    // {
    //     path: '/index',
    //     redirect: '/index',
    //     component: () => import( '@/views/home.vue'),
    //     children: [
    //         {
    //             path: '/index',
    //             name: 'Index',
    //             component: () => import('@/views/Index')
    //         },
    //         {
    //             path: '/aicreation',
    //             name: 'AICreation',
    //             component: () => import('@/views/AICreation')
    //         },
    //         {
    //             path: '/mine',
    //             name: 'Mine',
    //             component: () => import('@/views/Mine')
    //         }
    //     ]
    // },
    {
        path: '/aichat',
        name: 'Aichat',
        component: () => import( '@/views/aichat.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import( '@/views/login.vue')
    },
]

const router = new VueRouter({
    routes
})

// 全局前置路由守卫
router.beforeEach(async (to, from, next) => {
    const sessionId = localStorage.getItem('X-Token')  
    console.log("X-Token=",sessionId)
    if(!sessionId){
        //骚哥给接口拿到token
        Vue.prototype.$axios.get('/api/services/hzsfjtai/user/init', {}).then(res => {
            if (res.data.code === 200) {
              localStorage.setItem('X-Token', res.data.data);
            } else {
              this.$message(res.data.msg)
            }
          }).catch(err => {
            this.$message.error(err)
          })
        
    }
    if (to.path === '/login') {
        next({ path: '/home' })
    } else {
        next()
    }

    // let userInfo = JSON.parse(localStorage.getItem('userInfo'));
    // if (!userInfo && to.path !== '/login') {
    //     Message({
    //         message: '抱歉噢，请先登录~',
    //         type: 'warning'
    //     });
    //     next({ name: 'Name' })
    // } else {
    //     if (userInfo && to.path === '/login') {
    //         next({ path: '/home' })
    //     } else {
    //         next()
    //     }

    // }
    
})

export default router;

